<?php
/*
 * @Author: your name
 * @Date: 2020-05-22 16:12:24
 * @LastEditTime: 2020-05-27 11:04:42
 * @LastEditors: Please set LastEditors
 * @Description: 用户管理
 * @FilePath: /v3/application/adminapiv3/controller/userManage/UserController.php
 */ 
namespace app\adminapiv3\controller\userManage;

use app\adminapiv3\controller\Base;
use app\adminapiv3\service\userManage\UserService;
use think\facade\Request;
use think\facade\Validate;

class UserController extends Base
{
    /**
     * @name: 用户列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 16:28:55
     */
    function list ($userGroupId)
    {
        $params['real_name'] = $this->filterInput('real_name','post');
        $params['show_name'] = $this->filterInput('show_name','post');
        $params['email'] = $this->filterInput('email','post');
        $params['last_login_time'] = $this->filterInput('last_login_time','post');
        $params['last_login_ip'] = $this->filterInput('last_login_ip','post');

        $UserService = new UserService();
        $data = $UserService->list($params, $userGroupId);

        $this->jsonOutPut($data);
    }

    /**
     * @name: 重置密码
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-23 14:46:28
     */
    function resetPassword ()
    {
        global $logContent;
        $logContent = '重置密码';

        $user_id = $this->filterInput('user_id*', 'post');
        $password = $this->filterInput('password*', 'post');
        $confirm_password = $this->filterInput('confirm_password*', 'post');

        $UserService = new UserService();
        $UserService->resetPassword($user_id,$password,$confirm_password);

        $this->jsonOutPut();
    }

    /**
     * @name: 删除用户
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-23 15:09:17
     */
    function delete ()
    {
        global $logContent;
        $logContent = '删除用户';

        $user_id = $this->filterInput('id*', 'post');

        $UserService = new UserService();
        $UserService->delete($user_id);

        $this->jsonOutPut();
    }

    /**
     * @name: 修改用户信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-23 15:09:17
     */
    function modify ()
    {
        global $logContent;
        $logContent = '修改用户信息';

        $user_id = $this->filterInput('user_id*', 'post');
        $show_name = $this->filterInput('show_name*', 'post');
        $real_name = $this->filterInput('real_name*', 'post');
        $email = $this->filterInput('email*', 'post');

        $UserService = new UserService();
        $UserService->modify($user_id, $show_name, $real_name, $email);

        $this->jsonOutPut();
    }

    /**
     * @name: 添加用户
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-27 10:38:46
     */
    function add ()
    {
        global $logContent;
        $logContent = '添加用户';

        $input['user_group_id'] = $this->filterInput('user_group_id*', 'post');
        $input['show_name'] = $this->filterInput('show_name*', 'post');
        $input['real_name'] = $this->filterInput('real_name*', 'post');
        $input['email'] = $this->filterInput('email*', 'post');
        $input['password'] = $this->filterInput('password*', 'post');
        $input['confirm_password'] = $this->filterInput('confirm_password*', 'post');
        $input['reg_ip'] = Request::ip();

        $validate = Validate::make([
            'show_name' => 'require|chsAlphaNum|max:100',
            'real_name' => 'require|alphaDash|max:255',
            'email' => 'require|email',
            'password' => 'require|alphaNum|length:32',
            'confirm_password' => 'require|length:32|confirm:password'
        ]);
        if (!$validate->check($input))
        {
            jsonErrorOut($validate->getError(), $this->CODE_FAIL);
            return json_encode([
                'code' => $this->CODE_FAIL,
                'msg' => $validate->getError()
            ]);
        }
        unset($input['confirm_password']);

        $UserService = new UserService();
        $UserService->add($input);

        $this->jsonOutPut();
    }
}